package de.baumann.browser.adapter

import android.view.View
import android.widget.ImageView
import android.widget.ProgressBar
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import de.baumann.browser.OdinGlideModule
import de.baumann.browser.R
import de.baumann.browser.api.net.vo.duobao.DuoBao
import de.baumann.browser.utils.CountTimer3
import de.baumann.browser.utils.EndTimeCallback

class DuoBaoAdapter(var endTimeCallback: EndTimeCallback?) : BaseQuickAdapter<DuoBao, BaseViewHolder>(R.layout.item_duobao) {
    override fun convert(helper: BaseViewHolder, item: DuoBao) {
        helper.setText(R.id.duobaoqihao, "${item.phaseNum}期")
        helper.setText(R.id.duobaomingcheng, item.title)
        helper.setText(R.id.duobaojiage, "价值${item.totalValue}ODIN")
        val duobaojindu: ProgressBar = helper.getView(R.id.duobaojindu)
        duobaojindu.max = item.partNum
        duobaojindu.progress = item.partNum - item.residuePartNum
        val baifenbi = (item.partNum.toDouble() - item.residuePartNum.toDouble()) / item.partNum.toDouble()
        val duobaobili: TextView = helper.getView(R.id.duobaobili)

        duobaobili.text = "进度 ${(baifenbi * 100).toInt()}% 剩余 ${item.residuePartNum} 份"

        val duobaoCover = helper.getView<ImageView>(R.id.duobaofenmian)
        OdinGlideModule.load(mContext, item.thumbnail, duobaoCover, R.drawable.shape_cover)
        val totalFene: TextView = helper.getView(R.id.totalFene)
        totalFene.text = "总需份额${item.partNum}份"

        val duobaoStatus: ImageView = helper.getView(R.id.duobaoStatus)
        val btnLijiCanYu: TextView = helper.getView(R.id.btnLiJiCanYu)
        val yaoJiangDaoJiShi: TextView = helper.getView(R.id.yaojiangdaojishi)
        val cyfe: TextView = helper.getView(R.id.cyfe)
        val kaiqidaojishi: TextView = helper.getView(R.id.kaiqidaojishi)
        val kqdjs: TextView = helper.getView(R.id.kqdjs)
        when (item.operationStatus) {
            1, 2 -> {
                duobaoStatus.setImageResource(R.drawable.progress)
                btnLijiCanYu.visibility = View.VISIBLE
                yaoJiangDaoJiShi.visibility = View.INVISIBLE
                duobaojindu.visibility = View.VISIBLE
                duobaobili.visibility = View.VISIBLE
                totalFene.visibility = View.VISIBLE
                cyfe.visibility = View.INVISIBLE
                kaiqidaojishi.visibility = View.INVISIBLE
                kqdjs.visibility = View.INVISIBLE
            }

            3 -> {
                duobaoStatus.setImageResource(R.drawable.prize)
                btnLijiCanYu.visibility = View.INVISIBLE
                yaoJiangDaoJiShi.visibility = View.VISIBLE
                if (item.expectedTime > 0) {
                    CountTimer3(yaoJiangDaoJiShi, item.expectedTime).start()
                }
                duobaojindu.visibility = View.VISIBLE
                duobaobili.visibility = View.VISIBLE
                totalFene.visibility = View.VISIBLE
                cyfe.visibility = View.INVISIBLE
                kaiqidaojishi.visibility = View.INVISIBLE
                kqdjs.visibility = View.INVISIBLE
            }

            5 -> {
                duobaoStatus.setImageResource(R.drawable.daikaiqi)
                btnLijiCanYu.visibility = View.INVISIBLE
                yaoJiangDaoJiShi.visibility = View.INVISIBLE
                duobaojindu.visibility = View.INVISIBLE
                duobaobili.visibility = View.INVISIBLE
                totalFene.visibility = View.INVISIBLE
                cyfe.visibility = View.INVISIBLE
                kaiqidaojishi.visibility = View.VISIBLE
                kqdjs.visibility = View.VISIBLE
                CountTimer3(kqdjs, item.publishTime, object : EndTimeCallback {
                    override fun callback() {
                        item.operationStatus = 1
                        notifyItemChanged(helper.layoutPosition)
                    }
                }).start()
            }

            else -> {
                duobaoStatus.setImageResource(R.drawable.progress)
                btnLijiCanYu.visibility = View.INVISIBLE
                yaoJiangDaoJiShi.visibility = View.INVISIBLE
                duobaojindu.visibility = View.VISIBLE
                duobaobili.visibility = View.VISIBLE
                totalFene.visibility = View.VISIBLE
                cyfe.visibility = View.VISIBLE
                cyfe.text = "参与份额 ${item.participationNum}份"
                kaiqidaojishi.visibility = View.INVISIBLE
                kqdjs.visibility = View.INVISIBLE
            }
        }
    }
}